<?php
namespace app\api\controller;

use app\common\model\Reservation;
use app\common\service\Message;
use EasyWeChat\Factory;
use app\api\model\User as UserModel;
use think\Db;

/**
 *
 */
class Server
{
    private $app='';

    public function __construct(){
        $config = [
            'app_id' => 'wx8468f37d615992c0',
            'secret' => '70dab87efc8320480ab94c9826a56557',
            'token' => 'wx471e1e5dfb78fbb7',
            'aes_key'=>'dQHN7KcmVAlJflcWpmLN3It6UVIukcHjUpnLbB697p9',
            'response_type' => 'array',
        ];

        $this->app = Factory::officialAccount($config);
    }
    /**
     *
     */
    public function index()
    {
        // $res=$this->app->template_message->send([
        //     'touser' => 'o1vlg6aqs93gM6pFRoTlb14Sd4pg',
        //     'template_id' => 'EUAV3WMJQ3DB7QEgHyrkxVfhlg4f0jmhn79S1Lgpf0M',
        //     'url' => '',
        //     'miniprogram' => [
        //             'appid' => 'wx471e1e5dfb78fbb7',
        //             'pagepath' => 'pages/index/index',
        //     ],
        //     'data' => [
        //         'first'=>['value' => '约课测试', 'color' => '#550038'],
        //         'keyword1' => ['value' => '王圆圆', 'color' => '#550038'],
        //         'keyword2' => ['value' => '王芳', 'color' => '#550038'],
        //         'keyword3'=>['value' => '2023-03-29 19:00-20:00', 'color' => '#550038'],
        //         'remark'=>['value' => ''],
        //     ],
        // ]);

        // $info=$this->get();
        // echo '<pre>';
        // var_dump($info);
        // exit;
        // o1vlg6dXGWDubKrqyqmujx7ddRtg
        $list=$this->app->user->list($nextOpenId = null);  // $nextOpenId 可选
        foreach ($list['data']['openid'] as $k=>$v){
            if ($v=='o1vlg6dXGWDubKrqyqmujx7ddRtg'){
                break;
            }
            $this->get($v);
        }
        echo 'succes';
    }

    public function test(){
        $detail=UserModel::useGlobalScope(false)->where(['unionid'=>'oPjuq5in-P2C76TkGqGAbaZgQoPY'])->find();
        dump($detail);
    }

    public function get($open_id="o1vlg6UFcwZTb1j3Gm2Fgw98oh3U"){
        $wx_info=$this->app->user->get($open_id);
        $detail=UserModel::useGlobalScope(false)->where(['unionid'=>$wx_info['unionid']])->find();
        if ($detail) {
            echo $wx_info['openid'].'<br>';
            $detail->save(['gz_open_id'=>$wx_info['openid']]);
        }
        return true;
    }

    /**
     * @title 开课提醒小程序推送
     * @author Qian
     * Time: 2023/3/22  11:04
     */
    public function course(){
        $model = new Reservation();
        $list = $model->getRemainList();
        $list = $list->toArray();
        dump($list);
        if ($list){
            foreach ($list as $k=>$v){
                $res = Message::send('course',['reservation'=>$v]);
                if ($res){
                    //更新已发送
                    Db::name('course_reservation')->where(['id'=>$v['id']])->update(['is_remain'=>1]);
                }
            }
        }

    }
}
